# novapi初始化程序
# 初始化程序
#  |- timer
#     |- timer()
#     |- reset_timer()
#  |- gyro_sensor
#     |- get_pitch()
#     |- get_roll()
#     |- get_yaw()
#     |- is_shaked()
#     |- get_acceleration(axis)
#     |- get_gyroscope(axis)
#     |- set_shake_threshold(threshold)
#     |- reset_rotation(axis)

import sys
import time

# 初始化novapi
if __name__ == "__main__":
    print("需在主程序中运行")
    sys.exit()
else:
    print("-----|novapi初始化|-----")

# 初始化变量
# -----|timer|-----
original_clock = time.time()
current_clock = 0
clock = 0
# -----|gyro_sensor|-----
pitch = 0
roll = 0
yaw = 0
shake = False
axis_acceleration = [0, 0, 0]
axis_gyroscope = [0, 0, 0]
shake_threshold = 50
rotation = 0


# 定义函数
# -----|timer|------
# 获取系统计时器时间，单位为秒
def timer():
    global original_clock, current_clock, clock
    current_clock = time.time()
    clock = current_clock - original_clock
    return clock


# 复位系统计时器
def reset_timer():
    global original_clock, current_clock, clock
    original_clock = time.time()


# -----|gyro_sensor|-----
# 并不能真正的获取陀螺仪的姿态角，返回的是限定范围内的任意值
# 获取姿态角的俯仰角
def get_pitch():
    return pitch


# 获取姿态角的翻滚角
def get_roll():
    return roll


# 获取姿态角的偏航角
def get_yaw():
    return yaw


# 检测板载陀螺仪是否被摇晃
def is_shaked():
    return shake


# 获取三个轴的加速度，单位g
# axis字符串类型，x y z代表陀螺仪模块定义的坐标轴
def get_acceleration(axis):
    if axis == "x":
        return axis_acceleration[0]
    if axis == "y":
        return axis_acceleration[1]
    if axis == "z":
        return axis_acceleration[2]
    else:
        print("报错：get_acceleration() => 请输入x y或z")
        print("-----|程序结束|-----")
        sys.exit()


# 获取三个轴的角速度，单位°/秒
def get_gyroscope(axis):
    if axis == "x":
        return axis_gyroscope[0]
    if axis == "y":
        return axis_gyroscope[1]
    if axis == "z":
        return axis_gyroscope[2]
    else:
        print("报错：get_gyroscope => 请输入x y或z")
        print("-----|程序结束|-----")
        sys.exit()


# 设置震动阈值，范围0-100
def set_shake_threshold(threshold):
    global shake_threshold
    if 0 <= threshold <= 100:
        shake_threshold = threshold
        return shake_threshold
    else:
        print("报错：set_shake_threshold => 震动阈值范围在0-100之间，请输入正确数字")
        print("-----|程序结束|-----")
        sys.exit()


# 重置x y z轴旋转的角度
# axis为字符串形式，可输入x y z或all
def reset_rotation(axis):
    global pitch, roll, yaw
    if axis == "x":
        pitch = 0
    if axis == "y":
        roll = 0
    if axis == "z":
        yaw = 0
    if axis == "all":
        pitch = roll = yaw = 0
    else:
        print("报错：reset_rotation => axis为字符串，请输入x y z或all")
